<!DOCTYPE html>
<html lang="zh_cn">
  <head>
    
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>


<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">



  <meta name="description" content="微信公众号开发入门"/>




  <meta name="keywords" content="wechat, 阿辉的博客" />










  <link rel="alternate" href="/blog/default" title="阿辉的博客">




  <link rel="shortcut icon" type="image/x-icon" href="/blog/favicon.ico?v=2.9.0" />



<link rel="canonical" href="http://goodbox.gitee.io/blog/2020/03/24/gongzhonghaoceshi/"/>



  <link rel="stylesheet" type="text/css" href="/blog/lib/fancybox/jquery.fancybox.css" />




  <link rel="stylesheet" type="text/css" href="/blog/lib/nprogress/nprogress.min.css" />



<link rel="stylesheet" type="text/css" href="/blog/css/style.css?v=2.9.0" />



  



  <script id="baidu_push">
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>








<script>
  window.config = {"title":"阿辉的博客","subtitle":"","description":"","author":"阿辉","language":"zh_cn","timezone":"","url":"http://goodbox.gitee.io/blog","root":"/blog/","permalink":":year/:month/:day/:title/","permalink_defaults":null,"pretty_urls":{"trailing_index":true,"trailing_html":true},"source_dir":"source","public_dir":"public","tag_dir":"tags","archive_dir":"archives","category_dir":"categories","code_dir":"downloads/code","i18n_dir":":lang","skip_render":null,"new_post_name":":title.md","default_layout":"post","titlecase":false,"external_link":{"enable":true,"field":"site","exclude":""},"filename_case":0,"render_drafts":false,"post_asset_folder":true,"relative_link":false,"future":true,"highlight":{"enable":true,"auto_detect":false,"line_number":true,"tab_replace":"","wrap":true,"hljs":false,"first_line_number":"always1"},"default_category":"uncategorized","category_map":null,"tag_map":null,"date_format":"YYYY-MM-DD","time_format":"HH:mm:ss","use_date_for_updated":false,"per_page":10,"pagination_dir":"page","theme":"even","server":{"port":4000,"log":false,"compress":false,"header":true,"cache":false},"deploy":{"type":"git","repo":"https://gitee.com/goodbox/blog.git","branch":"master"},"ignore":null,"meta_generator":true,"keywords":null,"index_generator":{"per_page":10,"order_by":"-date","path":""},"include":null,"exclude":null,"menu":{"Home":"/","Archives":"/archives/","Categories":"/categories/","Tags":"/tags","About":"/about"},"leancloud":{"baidu_analytics":"9b96b92118600aa5dae45039cebb80de","app_id":null,"app_key":null},"social":{"email":"your@email.com","stack-overflow":null,"twitter":null,"facebook":null,"linkedin":null,"google":null,"github":"https://github.com/ahonn","weibo":null,"zhihu":null,"douban":null,"pocket":null,"tumblr":null,"instagram":null},"copyright":{"enable":true,"license":"<a rel=\"license\" href=\"http://creativecommons.org/licenses/by-nc/4.0/\" target=\"_blank\">知识共享署名-非商业性使用 4.0 国际许可协议</a>"},"archive_generator":{"per_page":10,"yearly":true,"monthly":true,"daily":false},"category_generator":{"per_page":10},"tag_generator":{"per_page":10},"marked":{"gfm":true,"pedantic":false,"breaks":true,"smartLists":true,"smartypants":true,"modifyAnchors":"","autolink":true,"sanitizeUrl":false,"headerIds":true},"since":2015,"favicon":"/favicon.ico","rss":"default","color":"Default","toc":true,"fancybox":true,"pjax":true,"reward":{"enable":false,"qrCode":{"wechat":null,"alipay":null}},"baidu_analytics":null,"baidu_verification":null,"google_analytics":null,"google_verification":null,"disqus_shortname":null,"changyan":{"appid":null,"appkey":null},"livere_datauid":null,"version":"2.9.0"};
</script>

    <title> 微信公众号开发入门 - 阿辉的博客 </title>
  <meta name="generator" content="Hexo 4.2.0"></head>

  <body><div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/blog/." class="logo">阿辉的博客</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>

<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    
      <a href="/blog/">
        <li class="mobile-menu-item">
          
          
            首页
          
        </li>
      </a>
    
      <a href="/blog/archives/">
        <li class="mobile-menu-item">
          
          
            归档
          
        </li>
      </a>
    
      <a href="/blog/categories/">
        <li class="mobile-menu-item">
          
          
            分类
          
        </li>
      </a>
    
      <a href="/blog/tags">
        <li class="mobile-menu-item">
          
          
            标签
          
        </li>
      </a>
    
      <a href="/blog/about">
        <li class="mobile-menu-item">
          
          
            关于
          
        </li>
      </a>
    
  </ul>
</nav>

    <div class="container" id="mobile-panel">
      <header id="header" class="header"><div class="logo-wrapper">
  <a href="/blog/." class="logo">阿辉的博客</a>
</div>

<nav class="site-navbar">
  
    <ul id="menu" class="menu">
      
        <li class="menu-item">
          <a class="menu-item-link" href="/blog/">
            
            
              首页
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/blog/archives/">
            
            
              归档
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/blog/categories/">
            
            
              分类
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/blog/tags">
            
            
              标签
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/blog/about">
            
            
              关于
            
          </a>
        </li>
      
    </ul>
  
</nav>

      </header>

      <main id="main" class="main">
        <div class="content-wrapper">
          <div id="content" class="content">
            
  
  <article class="post">
    <header class="post-header">
      <h1 class="post-title">
        
          微信公众号开发入门
        
      </h1>

      <div class="post-meta">
        <span class="post-time">
          2020-03-24
        </span>
        
          <div class="post-category">
            
              <a href="/blog/categories/wechat/">wechat</a>
            
          </div>
        
        
      </div>
    </header>

    
    


    <div class="post-content">
      
        <p>申请测试帐号<br>个人申请的公众号没有认证的话，是没有那么多权限的，不过微信给我们提供了一个申请测试帐号的页面，申请的测试帐号基本上有所有的权限，申请地址：<a href="https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&amp;t=sandbox/index" target="_blank" rel="noopener">https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&amp;t=sandbox/index</a></p>
<p>扫码登录一下，微信就会分配一个测试帐号</p>
<a id="more"></a>
<p>接入自己的服务器<br>官方文档地址：<a href="https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Access_Overview.html" target="_blank" rel="noopener">https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Access_Overview.html</a></p>
<p>我这用springboot启动了一个web服务，提供了一个接口，访问地址为 <a href="http://localhost:8080/wechat/，请求类型为GET，根据文档中介绍，微信来验证的时候会传四个参数" target="_blank" rel="noopener">http://localhost:8080/wechat/，请求类型为GET，根据文档中介绍，微信来验证的时候会传四个参数</a> signature timestamp nonce echostr</p>
<p>我自己服务器里这个接口拿到这四个参数后，要使用token、timestamp、nonce这三个参数进行校验，成功的话，将参数echostr返回就可以接入成功了</p>
<p>文档中给出的是php的校验方法，我在网上找了一个java的校验类</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><span class="line">import java.security.MessageDigest;</span><br><span class="line">import java.security.NoSuchAlgorithmException;</span><br><span class="line"></span><br><span class="line">&#x2F;**</span><br><span class="line"> * 请求校验工具类</span><br><span class="line"> *&#x2F;</span><br><span class="line">public class SignUtil &#123;</span><br><span class="line"></span><br><span class="line">    public static boolean checkSignature(String token, String signature, String timestamp, String nonce) &#123;</span><br><span class="line">        &#x2F;&#x2F;从请求中（也就是微信服务器传过来的）拿到的token, timestamp, nonce</span><br><span class="line">        String[] arr &#x3D; new String[]&#123;token, timestamp, nonce&#125;;</span><br><span class="line">        &#x2F;&#x2F; 将token、timestamp、nonce三个参数进行字典序排序</span><br><span class="line">        sort(arr);</span><br><span class="line">        StringBuilder content &#x3D; new StringBuilder();</span><br><span class="line">        for (int i &#x3D; 0; i &lt; arr.length; i++) &#123;</span><br><span class="line">            content.append(arr[i]);</span><br><span class="line">        &#125;</span><br><span class="line">        MessageDigest md &#x3D; null;</span><br><span class="line">        String tmpStr &#x3D; null;</span><br><span class="line"></span><br><span class="line">        try &#123;</span><br><span class="line">            md &#x3D; MessageDigest.getInstance(&quot;SHA-1&quot;);</span><br><span class="line">            &#x2F;&#x2F; 将三个参数字符串拼接成一个字符串进行sha1加密</span><br><span class="line">            byte[] digest &#x3D; md.digest(content.toString().getBytes());</span><br><span class="line">            &#x2F;&#x2F;将字节数组转成字符串</span><br><span class="line">            tmpStr &#x3D; byteToStr(digest);</span><br><span class="line">        &#125; catch (NoSuchAlgorithmException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        content &#x3D; null;</span><br><span class="line">        &#x2F;&#x2F; 将sha1加密后的字符串可与signature对比，标识该请求来源于微信</span><br><span class="line">        return tmpStr !&#x3D; null &amp;&amp; tmpStr.equals(signature.toUpperCase());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    &#x2F;&#x2F;将加密后的字节数组变成字符串</span><br><span class="line">    private static String byteToStr(byte[] byteArray) &#123;</span><br><span class="line">        String strDigest &#x3D; &quot;&quot;;</span><br><span class="line">        for (int i &#x3D; 0; i &lt; byteArray.length; i++) &#123;</span><br><span class="line">            strDigest +&#x3D; byteToHexStr(byteArray[i]);</span><br><span class="line">        &#125;</span><br><span class="line">        return strDigest;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private static String byteToHexStr(byte mByte) &#123;</span><br><span class="line">        char[] Digit &#x3D; &#123;&#39;0&#39;, &#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;, &#39;5&#39;, &#39;6&#39;, &#39;7&#39;, &#39;8&#39;, &#39;9&#39;, &#39;A&#39;,</span><br><span class="line">                &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;&#125;;</span><br><span class="line">        char[] tempArr &#x3D; new char[2];</span><br><span class="line">        tempArr[0] &#x3D; Digit[(mByte &gt;&gt;&gt; 4) &amp; 0X0F];</span><br><span class="line">        tempArr[1] &#x3D; Digit[mByte &amp; 0X0F];</span><br><span class="line"></span><br><span class="line">        String s &#x3D; new String(tempArr);</span><br><span class="line">        return s;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    &#x2F;&#x2F;用于字典排序</span><br><span class="line">    public static void sort(String a[]) &#123;</span><br><span class="line">        for (int i &#x3D; 0; i &lt; a.length - 1; i++) &#123;</span><br><span class="line">            for (int j &#x3D; i + 1; j &lt; a.length; j++) &#123;</span><br><span class="line">                if (a[j].compareTo(a[i]) &lt; 0) &#123;</span><br><span class="line">                    String temp &#x3D; a[i];</span><br><span class="line">                    a[i] &#x3D; a[j];</span><br><span class="line">                    a[j] &#x3D; temp;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>接口如下</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">@GetMapping(&quot;&#x2F;&quot;)</span><br><span class="line">public Object callback(String signature, String timestamp, String nonce, String echostr) &#123;</span><br><span class="line">    log.info(&quot;signature: &#123;&#125;, timestamp: &#123;&#125;, nonce: &#123;&#125;, echostr: &#123;&#125;&quot;, signature, timestamp, nonce, echostr);</span><br><span class="line">    if (SignUtil.checkSignature(siteConfig.getWechat().getToken(), signature, timestamp, nonce)) &#123;</span><br><span class="line">        return ResponseEntity.ok(echostr);</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">        return ResponseEntity.noContent();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>用户页面授权<br>公众号文档中的接口很多都要用户的openid，所以有必要在一开始就能拿到用户的openid，这样就可以给用户发消息，模板消息等</p>
<p>页面授权官方文档：<a href="https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html" target="_blank" rel="noopener">https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html</a></p>
<p>就是一套OAuth2协议的授权，这个不多说</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">用户授权拿到的数据除了openid外，还有access_token,这个token跟公众号的token是不一样的，这个token的作用是拿来调用用户的一些数据的，比如用户的基本信息</span><br><span class="line"></span><br><span class="line">另外还有一个接口 https:&#x2F;&#x2F;developers.weixin.qq.com&#x2F;doc&#x2F;offiaccount&#x2F;Basic_Information&#x2F;Get_access_token.html 拿的是公众号的token，这个token的作用是调用公众号的一些接口的，比如给用户发送消息等</span><br></pre></td></tr></table></figure>

<p>注意</p>
<p>如果是测试帐号的话，调用页面授权还要在测试帐号页面上配置一下域名，这个配置的地方有点不太容易发现，位置如下图，把上面接入的个人的服务域名写上就可以了</p>
<img src="/blog/2020/03/24/gongzhonghaoceshi/1.png" class title="联通查询二维码">

<p>发送模板消息<br>官方文档：<a href="https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html" target="_blank" rel="noopener">https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html</a></p>
<p>首先要先创建模板消息，如下图</p>
<img src="/blog/2020/03/24/gongzhonghaoceshi/2.png" class title="消息模版">

<p>其中 `` 里的内容是固定写法，keyword可以不止3个，可以更多，也可以更少</p>
<p>对应发送接口的数据长下面这个样</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    &quot;touser&quot;:&quot;OPENID&quot;,</span><br><span class="line">    &quot;template_id&quot;:&quot;ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY&quot;,</span><br><span class="line">    &quot;url&quot;:&quot;http:&#x2F;&#x2F;weixin.qq.com&#x2F;download&quot;,</span><br><span class="line">    &quot;data&quot;:&#123;</span><br><span class="line">            &quot;first&quot;: &#123;</span><br><span class="line">                &quot;value&quot;:&quot;恭喜你购买成功！&quot;,</span><br><span class="line">                &quot;color&quot;:&quot;#173177&quot;</span><br><span class="line">            &#125;,</span><br><span class="line">            &quot;keyword1&quot;:&#123;</span><br><span class="line">                &quot;value&quot;:&quot;巧克力&quot;,</span><br><span class="line">                &quot;color&quot;:&quot;#173177&quot;</span><br><span class="line">            &#125;,</span><br><span class="line">            &quot;keyword2&quot;: &#123;</span><br><span class="line">                &quot;value&quot;:&quot;39.8元&quot;,</span><br><span class="line">                &quot;color&quot;:&quot;#173177&quot;</span><br><span class="line">            &#125;,</span><br><span class="line">            &quot;keyword3&quot;: &#123;</span><br><span class="line">                &quot;value&quot;:&quot;2014年9月22日&quot;,</span><br><span class="line">                &quot;color&quot;:&quot;#173177&quot;</span><br><span class="line">            &#125;,</span><br><span class="line">            &quot;remark&quot;:&#123;</span><br><span class="line">                &quot;value&quot;:&quot;欢迎再次购买！&quot;,</span><br><span class="line">                &quot;color&quot;:&quot;#173177&quot;</span><br><span class="line">            &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>公众号token获取<br>公众号token最长有效期是2个小时，用于调用公众号的一些接口，一般简单点用个定时器来定时更新即可，不过也可以将token存在一个变量里，每次获取的时候，判断一下token是否过期，过期的话，重新请求更新token，下面是我封装的一个工具，交给spring管理</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">@Component</span><br><span class="line">@Slf4j</span><br><span class="line">public class WeChatUtil &#123;</span><br><span class="line"></span><br><span class="line">    private Date expireTime;</span><br><span class="line">    private String accessToken;</span><br><span class="line"></span><br><span class="line">    @Autowired</span><br><span class="line">    private RestTemplate restTemplate;</span><br><span class="line">    @Autowired</span><br><span class="line">    private SiteConfig siteConfig;</span><br><span class="line"></span><br><span class="line">    public String getAccessToken() &#123;</span><br><span class="line">        if (accessToken &#x3D;&#x3D; null || DateUtil.isExpire(expireTime)) &#123;</span><br><span class="line">            this.accessToken &#x3D; null;</span><br><span class="line">            this.fetchAssessToken();</span><br><span class="line">            return accessToken;</span><br><span class="line">        &#125;</span><br><span class="line">        return accessToken;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private void fetchAssessToken() &#123;</span><br><span class="line">        try &#123;</span><br><span class="line">            String appID &#x3D; siteConfig.getWechat().getAppID();</span><br><span class="line">            String appsecret &#x3D; siteConfig.getWechat().getAppsecret();</span><br><span class="line">            String url &#x3D; &quot;https:&#x2F;&#x2F;api.weixin.qq.com&#x2F;cgi-bin&#x2F;token?grant_type&#x3D;client_credential&amp;appid&#x3D;&quot; + appID + &quot;&amp;secret&#x3D;&quot; + appsecret;</span><br><span class="line">            ResponseEntity&lt;String&gt; forEntity &#x3D; restTemplate.getForEntity(url, String.class);</span><br><span class="line">            Map body &#x3D; JSON.parseObject(forEntity.getBody(), Map.class);</span><br><span class="line">            String access_token &#x3D; (String) body.get(&quot;access_token&quot;);</span><br><span class="line">            log.info(&quot;时间: &#123;&#125;, accessToken: &#123;&#125;&quot;, DateUtil.formatDateTime(new Date(), DateUtil.FORMAT_DATETIME), access_token);</span><br><span class="line">            this.expireTime &#x3D; DateUtil.getHourAfter(new Date(), 1);</span><br><span class="line">            this.accessToken &#x3D; access_token;</span><br><span class="line">        &#125; catch (RestClientException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">            log.error(&quot;时间：&#123;&#125;，获取accessToken失败！！&quot;, DateUtil.formatDateTime(new Date(), DateUtil.FORMAT_DATETIME));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>用到的工具类</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;**</span><br><span class="line">* 判断传入的时间是否在当前时间之后，返回boolean值</span><br><span class="line">* true: 过期</span><br><span class="line">* false: 还没过期</span><br><span class="line">*</span><br><span class="line">* @param date</span><br><span class="line">* @return</span><br><span class="line">*&#x2F;</span><br><span class="line">public static boolean isExpire(Date date) &#123;</span><br><span class="line">    if (date.before(new Date())) return true;</span><br><span class="line">    return false;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>总结<br>有了上面这些就可以做一个 Server酱 了！</p>

      
    </div>

    
      
      

  <div class="post-copyright">
    <p class="copyright-item">
      <span>原文作者: </span>
      <a href="http://goodbox.gitee.io/blog">阿辉</a>
    </p>
    <p class="copyright-item">
      <span>原文链接: </span>
      <a href="http://goodbox.gitee.io/blog/2020/03/24/gongzhonghaoceshi/">http://goodbox.gitee.io/blog/2020/03/24/gongzhonghaoceshi/</a>
    </p>
    <p class="copyright-item">
      <span>许可协议: </span>
      
      <a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/" target="_blank">知识共享署名-非商业性使用 4.0 国际许可协议</a>
    </p>
  </div>



      
      
    

    
      <footer class="post-footer">
        
          <div class="post-tags">
            
              <a href="/blog/tags/wechat/">wechat</a>
            
          </div>
        
        
        
  <nav class="post-nav">
    
      <a class="prev" href="/blog/2020/03/24/spring-boot-plugin/">
        <i class="iconfont icon-left"></i>
        <span class="prev-text nav-default">spring-boot 项目利用spring切面实现插件功能，对项目无侵入</span>
        <span class="prev-text nav-mobile">上一篇</span>
      </a>
    
    
      <a class="next" href="/blog/2020/03/24/epidemicQuery/">
        <span class="next-text nav-default">中国联通、电信、移动手机卡查询个人轨迹行程查询疫情期间行程查询 - 扫一扫，查询最近14日内经过的地方</span>
        <span class="prev-text nav-mobile">下一篇</span>
        <i class="iconfont icon-right"></i>
      </a>
    
  </nav>

      </footer>
    

  </article>


          </div>
          
  <div class="comments" id="comments">
    
  </div>


        </div>
      </main>

      <footer id="footer" class="footer">

  <div class="social-links">
    
      
        
          <a href="mailto:your@email.com" class="iconfont icon-email" title="email"></a>
        
      
    
      
    
      
    
      
    
      
    
      
    
      
        
          <a href="https://github.com/ahonn" target="_blank" rel="noopener" class="iconfont icon-github" title="github"></a>
        
      
    
      
    
      
    
      
    
      
    
      
    
      
    
    
    
      
      <a href="/blog/atom.xml" class="iconfont icon-rss" title="rss"></a>
    
  </div>


<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://hexo.io/" target="_blank" rel="noopener">Hexo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/ahonn/hexo-theme-even" target="_blank" rel="noopener">Even</a>
  </span>

  <span class="copyright-year">
    
    &copy; 
     
      2015 - 
    
    2020

    <span class="heart">
      <i class="iconfont icon-heart"></i>
    </span>
    <span class="author">阿辉</span>
  </span>
</div>

      </footer>

      <div class="back-to-top" id="back-to-top">
        <i class="iconfont icon-up"></i>
      </div>
    </div>

    
  
  

  



    
  



  
  





  
    <script type="text/javascript" src="/blog/lib/jquery/jquery.min.js"></script>
  

  
    <script type="text/javascript" src="/blog/lib/slideout/slideout.js"></script>
  

  
    <script type="text/javascript" src="/blog/lib/fancybox/jquery.fancybox.pack.js"></script>
  

  
    <script type="text/javascript" src="/blog/lib/pjax/jquery.pjax.min.js"></script>
  

  
    <script type="text/javascript" src="/blog/lib/nprogress/nprogress.min.js"></script>
  


    <script type="text/javascript" src="/blog/js/src/even.js?v=2.9.0"></script>

  </body>
</html>
